Skip to content

Comments

[tkddbs587] 25.01.16#27

Merged
JooKangsan merged 18 commits intomainfrom
tkddbs587
Jan 23, 2025
Merged

[tkddbs587] 25.01.16#27
JooKangsan merged 18 commits intomainfrom
tkddbs587

Conversation

@tkddbs587
Copy link
Collaborator

Queue

📌 푼 문제

문제이름 문제링크
순서쌍의 개수 https://school.programmers.co.kr/learn/courses/30/lessons/120836
점의 위치 구하기 https://school.programmers.co.kr/learn/courses/30/lessons/120841
로그인 성공? https://school.programmers.co.kr/learn/courses/30/lessons/120883
카드 뭉치 https://school.programmers.co.kr/learn/courses/30/lessons/159994
햄버거 만들기 https://school.programmers.co.kr/learn/courses/30/lessons/133502

📝 간단한 풀이 과정

순서쌍의 개수

function solution(n) {
  let count = 0;

  for (let i = 1; i <= n; i++) {
    if (n % i === 0) {
      // n의 약수 구하기
      count++; // 약수 구할때마다 카운트업
    }
  }

  return count; // 순서쌍 갯수 반환
}

점의 위치 구하기

function solution(dot) {
  const [x, y] = dot; // x, y 값

  if (Math.sign(x) === 1) {
    // x값이 양수일때
    if (Math.sign(y) === 1) {
      // y값도 양수면
      return 1; // 1사분면
    } else {
      return 4; // x값은 양수고 y값은 음수 -> 4사분면
    }
  }

  if (Math.sign(x) === -1) {
    // x값이 음수일때
    if (Math.sign(y) === -1) {
      // y값도 음수면
      return 3; // 3사분면
    } else {
      return 2; // y값은 양수고 x값은 음수 -> 2사분면
    }
  }
}

로그인 성공?

function solution(id_pw, db) {
  const [id, pw] = id_pw; // 아이디, 패스워드 분리
  const map = new Map(db); // db를 기반으로 Map 객체 생성
  console.log(map);

  if (map.has(id)) {
    // map 객체에 id 가 있을때
    if (map.get(id) === pw) {
      // 아이디와 패스워드가 일치하면
      return "login"; // 'login' 반환
    } else return "wrong pw"; // 아이디는 같은데 패스워드만 틀리면 'wrong pw' 반환
  } else return "fail"; // 둘 다 틀리면 'fail' 반환
}

카드 뭉치

function solution(cards1, cards2, goal) {
  let index1 = 0; // 카드1의 인덱스
  let index2 = 0; // 카드2의 인덱스

  for (let i = 0; i < goal.length; i++) {
    if (cards1[index1] === goal[i]) {
      // 카드1의 인덱스 0부터 순서대로 골의 인덱스 i와 같으면
      index1++; // 카드 1의 인덱스 1 증가
    } else if (cards2[index2] === goal[i]) {
      // 카드2의 인덱스 0부터 순서대로 골의 인덱스 i와 같으면
      index2++; // 카드 2의 인덱스 1 증가
    } else return "No"; // 순서대로 비교하다가 더 이상 일치하는 인덱스가 없으면 만들 수 없는 문장이므로 No 반환
  }
  return "Yes"; // goal의 인덱스만큼 순회를 성공적으로 마치면 문장을 만들 수 있으므로 Yes 반환
}

햄버거 만들기

function solution(ingredient) {
  let stack = []; // 스택
  let count = 0; // 햄버거 포장 갯수
  for (let i = 0; i < ingredient.length; i++) {
    stack.push(ingredient[i]); // ingredient 인덱스 순서대로 스택에 추가
    if (stack.length >= 4) {
      // 스택에 재료가 4개 이상 쌓였을때
      const hamburger = stack.slice(-4).join(""); // 스택의 끝에서부터 4개의 요소 문자열로 추출

      if (hamburger === "1231") {
        // 추출한 문자열이 1231 순서와 일치하면
        count++; // 포장 갯수 1 증가
        stack.splice(-4); // 스택에서 포장된 햄버거 제거
      }
    }
  }
  return count; // 총 포장 갯수 반환
}

@tkddbs587 tkddbs587 self-assigned this Jan 17, 2025
Copy link
Collaborator

@JooKangsan JooKangsan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

독감때문에 일주일내내 누워있어서 코드리뷰가늦었네요...죄송합니다.
수고하셨어요!!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function solution(dot) {
  const [x, y] = dot;
  
  switch (true) {
    case (x > 0 && y > 0):
      return 1;
    case (x < 0 && y > 0):
      return 2;
    case (x < 0 && y < 0):
      return 3;
    case (x > 0 && y < 0):
      return 4;
    default:
      throw new Error('Invalid coordinates');
  }
}

이런식으로 스위치 문을 사용해서 표헌하는 방법도 있어요!

@JooKangsan JooKangsan merged commit 22c3f15 into main Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants